package person.bing.fbcloud.leetcode;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 350. 两个数组的交集 II
 *
 * @author: mengfanbing
 * @date: 2020/7/13 15:17
 */
public class Q350 {
    public static void main(String[] args) {
        int[] nums1 = {1,2,2,1};
        int[] nums2 = {2,1};
        int[] intersect = intersect(nums1, nums2);
        System.out.println(Arrays.toString(intersect));
    }

    public static int[] intersect(int[] nums1, int[] nums2) {
        List<Integer> list = new ArrayList<>();
        List<Integer> list1 = Arrays.stream(nums1).boxed().collect(Collectors.toList());
        List<Integer> list2 = Arrays.stream(nums2).boxed().collect(Collectors.toList());
        for (Integer i:list1){
            if (list2.contains(i)){
                list.add(i);
                list2.remove(i);
            }
        }
        int[] ans = list.stream().mapToInt(Integer::valueOf).toArray();
        return ans;
    }
}
